package com.spit.tph.nike_mvp.adapter

import android.content.Context
import android.support.v4.content.ContextCompat
import android.support.v7.widget.AppCompatTextView
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Filter
import android.widget.Filterable
import android.widget.LinearLayout
import com.csl.ams.nike_mvp.recyclerview.ViewHolder
import com.flyco.roundview.RoundLinearLayout
import com.spit.tph.R
import com.yc.reid.adapter.BaseRecyclerviewAdapter
import com.yyc.stocktake.bean.DataBean
import com.zhy.view.flowlayout.FlowLayout
import com.zhy.view.flowlayout.TagFlowLayout
import com.zhy.view.flowlayout.TagFlowLayout.OnTagClickListener

/**
 * User: Nike
 *  2024/2/23 17:53
 */
class ConsumablesAdapter(act: Context, listBean: List<DataBean>) :
    BaseRecyclerviewAdapter<DataBean>(act, listBean), Filterable {

    override fun onCreateViewHolde(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
            LayoutInflater.from(parent.context).inflate(R.layout.i_consumables, parent, false)
        )
    }

    override fun onBindViewHolde(viewHolder: ViewHolder, position: Int) {
        val bean = mFilterList[position]
        bean.run {
            val layout = viewHolder.getView<RoundLinearLayout>(R.id.layout)
            val tvStatus = viewHolder.getView<AppCompatTextView>(R.id.tv_status)
            viewHolder.setText(R.id.tv_title, consumablesNo + "/" + consumablesName)
            viewHolder.setText(R.id.tv_specification, specs)
            viewHolder.setText(R.id.tv_brand, brand)
            viewHolder.setText(R.id.tv_current, inventoryLower)
            viewHolder.setText(R.id.tv_limited_use, singleClaimLimit)
            val unlimited = act.getString(R.string.unlimited)

            val inventory = bean.Inventory
            var list0 = inventory.filter  { it.locFlag == 0 }
            var list1 = inventory.filter  { it.locFlag == 1 }

            var flWarehouse = viewHolder.getView<TagFlowLayout>(R.id.fl_warehouse)
            flWarehouse.adapter = FlowTagAdapter(act, list0)
            viewHolder.getView<LinearLayout>(R.id.ly_wheelchair).visibility = if (list0.size != 0)View.VISIBLE else View.GONE

            var flMaterial = viewHolder.getView<TagFlowLayout>(R.id.fl_material)
            flMaterial.adapter = FlowTagAdapter(act, list1)
//            viewHolder.getView<LinearLayout>(R.id.ly_materialPoints).visibility = if (list1.size != 0)View.VISIBLE else View.GONE

            // Determine visibility based on any matching condition
            val showStatus1 = list1.any { inventoryLower != 0 && it.currentInventory <= inventoryLower }
            viewHolder.setViewVisibility(R.id.tv_status1, if (showStatus1) View.VISIBLE else View.GONE)

            viewHolder.setText(R.id.tv_limited_use, "${if (singleClaimLimit == 0) unlimited else singleClaimLimit}")
            viewHolder.setText(R.id.tv_current, "${if (inventoryLower == 0) unlimited else inventoryLower}")
            val delegate = layout.delegate
            delegate.backgroundColor = if (status == 1) ContextCompat.getColor(act, R.color.green_E1F7EE) else ContextCompat.getColor(act, R.color.ams_grey)
        }
        viewHolder.setOnItemClickListener(object : View.OnClickListener {
            override fun onClick(p0: View?) {
                mItemClickListener?.onItemClick(position, bean, p0!!)
            }
        })
        viewHolder.setOnItemLongClickListener(object :View.OnLongClickListener{
            override fun onLongClick(p0: View?): Boolean {
                mItemLongClickListener?.onItemClick(position, bean, p0!!)
                return true
            }
        })
    }

    var mFilterList = ArrayList<DataBean>()

    fun appendList(list: List<DataBean>) {
        listBean = list as MutableList<DataBean>
        //这里需要初始化filterList
        mFilterList = list as ArrayList<DataBean>
    }

    override fun getFilter(): Filter {
        return object : Filter() {
            //执行过滤操作
            override fun performFiltering(charSequence: CharSequence): FilterResults {
                val charString = charSequence.toString()
                if (charString.isEmpty()) {
                    //没有过滤的内容，则使用源数据
                    mFilterList = listBean as ArrayList<DataBean>
                } else {
                    val filteredList: MutableList<DataBean> = ArrayList()
                    for (i in listBean.indices) {
                        val bean = listBean[i]
                        val epc = bean.consumablesNo
                        val assetNo = bean.consumablesName
                        if (
                            epc?.contains(charString, ignoreCase = true) == true
                            || assetNo?.contains(charString, ignoreCase = true) == true
                        ) {
                            filteredList.add(bean)
                        }
                    }
                    mFilterList = filteredList as ArrayList<DataBean>
                }
                val filterResults = FilterResults()
                filterResults.values = mFilterList
                return filterResults
            }

            //把过滤后的值返回出来
            override fun publishResults(charSequence: CharSequence, filterResults: FilterResults) {
                mFilterList = filterResults.values as ArrayList<DataBean>
                notifyDataSetChanged()
                /*val filteredList = filterResults.values as? ArrayList<DataBean>
                if (filteredList != null) {
                    mFilterList = filteredList
                    notifyDataSetChanged()
                }*/
            }
        }
    }

    override fun getItemCount(): Int {
        return mFilterList.size
    }

    private var mItemClickListener: OnItemClickListener? = null

    fun setOnItemClickListener(l: OnItemClickListener?) {
        mItemClickListener = l
    }

    interface OnItemClickListener {
        fun onItemClick(position: Int, item: DataBean, view: View)
    }

    private var mItemLongClickListener: OnItemLongClickListener? = null

    fun setOnItemLongClickListener(l: OnItemLongClickListener?) {
        mItemLongClickListener = l
    }

    interface OnItemLongClickListener {
        fun onItemClick(position: Int, item: DataBean, view: View)
    }

}